package com.zhongke.cotrun.common.util;

import com.zhongke.cotrun.model.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.*;

@Component
public class AddIntegralUtils {
    @Autowired
    IntegralUtil integralUtil;

    /**
     * 积分规则生效
     *
     * @param port       //接口
     * @param membership //会员等级
     * @param userType   /用户类型
     * @param userId     //用户id
     * @param session    //
     * @return
     * @throws ParseException
     */
    public Map addIntegral(String port, String membership, List<String> userType, Integer userId, String historyExplain, HttpSession session) throws ParseException {
        Map map = new HashMap();
        integralUtil.addIntegral(port, membership, userType, userId, historyExplain, session);
        map.put("count", session.getAttribute("value"));

//        System.out.println("wo"+(map));

        return map;
    }


    /**
     * 积分生效根据用户的身份
     * 用户身份分解工具类
     *
     * @param userInfo
     * @param session
     * @param u
     * @return
     * @throws ParseException
     */
    public Map getMapOfUserEntity(UserInfo userInfo, Integer u, String port, String membership, String historyExplain, HttpSession session) throws ParseException {
        Map m = null;
        //u=0就持续触发
        if (u == null || u == 0) {
            String identity = userInfo.getIdentity();
            List<String> l = new ArrayList<>();

            if (identity == null || "".equals(identity)) {
                if ("1".equals(userInfo.getType())) {
                    l.add("普通用户");
                }else {
                    l.add("机构");
                }

            } else {
                if ("2".equals(userInfo.getType())) {
                    identity += ",机构";
                }
                String[] s = identity.split(",");
                l = Arrays.asList(s);
            }

            m = addIntegral(port, membership, l, userInfo.getUserId(), historyExplain, session);
//            m = addIntegral("/user/updateUserinfo", "VIP0", l, userInfo.getUserId(), "用户完善信息的时候增加:", session);

        }
        return m;
    }


}
